package com.situ.mall.controller;

import com.situ.common.util.Result;
import com.situ.mall.pojo.entity.Evaluate;
import com.situ.mall.pojo.entity.OrderItem;
import com.situ.mall.pojo.entity.Shipping;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.pojo.vo.EvaluateVO;
import com.situ.mall.service.IEvaluateService;
import com.situ.mall.service.IUserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/evaluate")
public class EvaluateController {
    @Autowired
    private IEvaluateService evaluateService;
    @Autowired
    private IUserService userService;
    @RequestMapping("/add")
    @ResponseBody
    public Result add(Evaluate evaluate, Long orderNo, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        User userInfo = userService.selectByInfoId(userId);
        if (ObjectUtils.isEmpty(userInfo.getName()) || ObjectUtils.isEmpty(userInfo.getImage())) {
            return Result.error("请先去修改用户名与头像！");
        } else {
            evaluate.setUserId(userId);
            if (ObjectUtils.isEmpty(evaluate.getRating())) {
                return Result.error("请添加评论评分！");
            } else if (ObjectUtils.isEmpty(evaluate.getEvaluateImage())) {
                return Result.error("请添加评论图片！");
            } else if (ObjectUtils.isEmpty(evaluate.getEvaluate())) {
                return Result.error("请添加评论！");
            } else {
                evaluateService.add(evaluate, orderNo);
                return Result.ok("评论成功！");
            }
        }


    }
    @RequestMapping("/selectByProductId")
    @ResponseBody
    public Result selectByProductId(Integer productId) {
        List<EvaluateVO> list = evaluateService.selectByProductId(productId);
        return Result.ok(list);
    }
}
